package formulation.controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.user.User;
import db.user.UserService;

public class UserController extends MultiActionController
{
    private UserService userService;
    
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public ModelAndView register(HttpServletRequest request,
            HttpServletResponse response)
    {
        return new ModelAndView("user/register");
    }
    
    public void checkName(HttpServletRequest request,
            HttpServletResponse response) throws IOException
    {
        String name = request.getParameter("name");
        User user = userService.findByName(name);
        if (user == null) {
            response.getWriter().print("unused");
        }
        else {
            response.getWriter().print("used");
        }
    }
    
    public ModelAndView registerService(HttpServletRequest request,
            HttpServletResponse response, User user)
    {
        String admin[] = request.getParameterValues("admin");
        Boolean isAdmin = Boolean.FALSE;
        if (admin != null)
            isAdmin = Boolean.TRUE;
        userService.registerUser(user.getName(), user.getPassword(), user.getEmail(), isAdmin);
        User act_user = userService.findByName(user.getName());
        if (act_user.getPassword().equals(user.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute("name", act_user.getName());
            session.setAttribute("admin", act_user.getAdmin());
        }
        return new ModelAndView("redirect:/index.html");
    }
    
    public ModelAndView signIn(HttpServletRequest request,
            HttpServletResponse response)
    {
        return new ModelAndView("user/signIn");
    }
    
    public ModelAndView signInService(HttpServletRequest request,
            HttpServletResponse response, User user)
    {
        // sign in logic
        User act_user = userService.findByName(user.getName());
  
       if (act_user!=null&&act_user.getPassword().equals(user.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute("name", act_user.getName());
            session.setAttribute("admin", act_user.getAdmin());
            return new ModelAndView("redirect:/index.html");
        }
       request.setAttribute("error","1");
       return new ModelAndView("user/signIn");
    }
    
    public ModelAndView signOutService(HttpServletRequest request,
            HttpServletResponse response)
    {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return new ModelAndView("redirect:/index.html");
    }
}
